﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Touch2Like.Model;
using System.Data;

namespace Touch2Like.DAL
{
    public class ShippingAddressDAL
    {
        #region Add
        public int Add(ShippingAddressInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeOutParam("@ListID",SqlDbType.Int,4),
                db.MakeInParam("@OrderID",SqlDbType.Int,4, info.OrderID),
                db.MakeInParam("@FirstName",SqlDbType.VarChar,50,info.FirstName),
                db.MakeInParam("@LastName",SqlDbType.VarChar,50,info.LastName),
                db.MakeInParam("@BusinessName",SqlDbType.VarChar,200,info.BusinessName),
                db.MakeInParam("@AddressLine1",SqlDbType.VarChar,200,info.AddressLine1),
                db.MakeInParam("@AddressLine2",SqlDbType.VarChar,200,info.AddressLine2),
                db.MakeInParam("@City",SqlDbType.VarChar,50,info.City),
                db.MakeInParam("@State",SqlDbType.VarChar,50,info.State),
                db.MakeInParam("@Zipcode",SqlDbType.VarChar,50,info.Zipcode),
                db.MakeInParam("@Country",SqlDbType.VarChar,50,info.Country),
                db.MakeInParam("@Phone",SqlDbType.VarChar,50,info.Phone),
                db.MakeInParam("@Email",SqlDbType.VarChar,200,info.Email)
            };
            try
            {
                db.ExecNonQuery("CAM_ShippingAddress_Add", param);
                return DataHelper.GetInt(param[0].Value).Value;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Update
        public void Update(ShippingAddressInfo info)
        {
            Database db = new Database();
            SqlParameter[] param =
            {
                db.MakeInParam("@ListID",SqlDbType.Int,4,info.ListID),
                db.MakeInParam("@OrderID",SqlDbType.Int,4, info.OrderID),
                db.MakeInParam("@FirstName",SqlDbType.VarChar,50,info.FirstName),
                db.MakeInParam("@LastName",SqlDbType.VarChar,50,info.LastName),
                db.MakeInParam("@Company",SqlDbType.VarChar,200,info.BusinessName),
                db.MakeInParam("@AddressLine1",SqlDbType.VarChar,200,info.AddressLine1),
                db.MakeInParam("@AddressLine22",SqlDbType.VarChar,200,info.AddressLine2),
                db.MakeInParam("@City",SqlDbType.VarChar,50,info.City),
                db.MakeInParam("@Zipcode",SqlDbType.VarChar,50,info.Zipcode),
                db.MakeInParam("@Country",SqlDbType.VarChar,50,info.Country),
                db.MakeInParam("@State",SqlDbType.VarChar,50,info.State),
                db.MakeInParam("@Phone",SqlDbType.VarChar,50,info.Phone),
                db.MakeInParam("@Email",SqlDbType.VarChar,200,info.Email),
                db.MakeInParam("@Status",SqlDbType.Int,4, info.Status)
            };
            try
            {
                db.ExecNonQuery("CAM_ShippingAddress_Update", param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion

        #region Get
        public ShippingAddressInfo GetByID(int listID)
        {
            Database db = new Database();
            SqlDataReader dr = null;
            SqlParameter[] param =
                {
                   db.MakeInParam("@ListID",SqlDbType.Int,4, listID)
                };

            try
            {
                dr = db.RunProc("CAM_ShippingAddress_GetByID", param);

                if (dr.Read())
                {
                    ShippingAddressInfo info = new ShippingAddressInfo();

                    info.ListID = DataHelper.GetInt(dr["ListID"]);
                    info.OrderID = DataHelper.GetInt(dr["OrderID"]);
                    info.FirstName = DataHelper.GetString(dr["FirstName"]);
                    info.LastName = DataHelper.GetString(dr["LastName"]);
                    info.BusinessName = DataHelper.GetString(dr["BusinessName"]);
                    info.AddressLine1 = DataHelper.GetString(dr["AddressLine1"]);
                    info.AddressLine2 = DataHelper.GetString(dr["Addressline2"]);
                    info.City = DataHelper.GetString(dr["City"]);
                    info.Zipcode = DataHelper.GetString(dr["Zipcode"]);
                    info.Country = DataHelper.GetString(dr["Country"]);
                    info.State = DataHelper.GetString(dr["State"]);
                    info.Phone = DataHelper.GetString(dr["Phone"]);
                    info.Email = DataHelper.GetString(dr["Email"]);
                    info.Status = DataHelper.GetInt(dr["Status"]);
                    info.CreateDate = DataHelper.GetDateTime(dr["CreateDate"]);
                    info.UpdateDate = DataHelper.GetDateTime(dr["UpdateDate"]);

                    return info;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dr.Close();
                db.Close();
            }
        }
        #endregion

        #region Get By OrderID
        public DataTable GetByOrderID(int orderid)
        {
            Database db = new Database();
            SqlParameter[] param =
                {
                   db.MakeInParam("@OrderID",SqlDbType.Int,4, orderid)
                };

            try
            {
                return db.GetDataSet("CAM_ShippingAddress_GetByOrderID", param).Tables[0];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                db.Close();
            }
        }
        #endregion
    }
}
